package services.dwzkdj;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.*;
import com.*;
/**
 * 文件上传Service
 * Wed Jan 27 14:25:04 CST 2016 gjx
 */ 
public class FileloadServices {
	static PublicSystem sys = PublicSystem.getInstance();
	/**
	 * 插入临时表
	 * @param list
	 * @param type
	 * @return
	 * @throws Exception
	 */
	public String insertsql(List list, String type) throws Exception {
		int num = 0;
		int count = 0;
		Connection con = sys.getConnection();
		int size = list.size();
		String eventtype = type;
		String ls = list.get(0).toString();
		ls = ls.substring(1, ls.length() - 1);
		String result[] = ls.split(",");
		String sql = "eventtype,eventid,";
		String value = "?,?,";
		for (int a = 0; a < result.length; a++) {
			int m = a + 1;
			sql += "c" + m + ",";
			value += "?,";
		}
		sql = sql.substring(0, sql.length() - 1);
		value = value.substring(0, value.length() - 1);
		Map map = new HashMap();
		String[] str = sys.callFunction("ZJ_GET_EVENTID", map).split("&");
		String eventid = str[0];
		PreparedStatement stmt = con.prepareStatement("insert into zj_tmp" + "(" + sql + ")" + "values" + "(" + value + ")");
		try {
			for(int i = 1; i < size; i++) {
				count++;  
				String ss = list.get(i).toString();
				ss=ss.substring(1, ss.length() - 1);
				String res[] = ss.split(",");
				stmt.setString(1, eventtype);
				stmt.setString(2, eventid);
				for (int a = 0; a < res.length; a++) {
					stmt.setString(a + 3, res[a].trim());
				}
				stmt.addBatch();
				if(count%2000 == 0) {   //批处理再提交  
					stmt.executeBatch();
					con.commit();
					stmt.close();
					con.close();
					con = sys.getConnection();
					stmt = con.prepareStatement("insert into zj_tmp" + "(" + sql + ")" + "values" + "(" + value + ")");
				}
			} 
			stmt.executeBatch();
			con.commit();
			stmt.close();
			con.close();
			return eventid;
		} catch(Exception d) {	
			con.rollback();
			stmt.close();
			con.close();
			d.printStackTrace();
		} finally {
			if (stmt != null) {  
				stmt.close();  
			}  
			if (con != null && !con.isClosed()) {  
				con.close();  
			}  
		}
		return eventid;
	}
}